package cn.sunan.org.controller;

import cn.sunan.basic.uitl.Ajaxresult;
import cn.sunan.basic.uitl.PageList;
import cn.sunan.org.domain.Employee;
import cn.sunan.org.query.EmployeeQuery;
import cn.sunan.org.service.IEmployeeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")

public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping
    public List<Employee> quertAll(){
        return employeeService.selectAll();
    }
    @GetMapping("/{id}")
    public  Employee selectById(@PathVariable("id") Long id){
        return employeeService.selctById(id);
    }
    @PostMapping
    public PageList<Employee> selectquery(@RequestBody EmployeeQuery employee){
        System.out.println(employee.getStart());
        System.out.println(employee.getCurrentPage());
       return   employeeService.queryByPage(employee);
    }
    @PutMapping
    public  Ajaxresult saveOrupdate(@RequestBody Employee dmployee) {
        try {
            if (dmployee.getId()==null)
                employeeService.save(dmployee);
            else {
                employeeService.update(dmployee);
            }
            return  new Ajaxresult();
        } catch (Exception e) {
            e.printStackTrace();
            return new Ajaxresult(false,"操作失败");
        }

    }
    @DeleteMapping("/{id}")
    public Ajaxresult delete(@PathVariable("id") Long id){
         try {
             employeeService.delete(id);
             return new Ajaxresult();
         }catch (Exception e){
            e.printStackTrace();
            return  new Ajaxresult(false,"操作失败");
         }

    }
    @PatchMapping
    public Ajaxresult delete1(@RequestBody List<Long> ids){
        try {
                employeeService.batchDetele(ids);
                return new Ajaxresult();
        }catch (Exception e){
            e.printStackTrace();
            return  new Ajaxresult(false,"删除失败");
        }
    }
}
